<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter :list="topFilterList" :cols="4" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <!-- 工单信息 -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :height="'500'"
      :selectionType="'single'"
      columnsRef="khtsXzgdGdxxTable"
      :onRowSelectChange="arr => (this.choose = arr)"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <div style="text-align:center">
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="req">确 定</el-button>
        <el-button type="primary" @click="cancle">取 消</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { searchRepairOrder, getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode } from '@/api/common/empSelect/index';

export default {
  name: 'ChooseRO',
  components: {},
  mixins: [dictionary],
  props: ['vinData'],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: searchRepairOrder, // 获取工单表格接口
      fetchParams: { xhrAbort: true }, // 获取工单表格参数
      columns: this.createTableColumns(), // 工单信息表格列
      choose: [] // 选中的工单
    };
  },

  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.setDefultCondition(this.vinData);
    this.getEmp();
  },
  methods: {
    async initSelect() {
      let testDrive = { role: dictCode.SERVICE_CONSULTANT };
      let res5 = (await searchRoleByCode(testDrive)).data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.columns.find(x => x.dataIndex === 'sa').editItems =  res5;
      this.columns.find(x => x.dataIndex === 'ct').editItems =  res5;
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'roNo',
          placeholder: '工单号'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          placeholder: '车牌号'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: 'VIN'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '工单号',
          dataIndex: 'RO_NO',
          width:150,
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_NAME',
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '维修类型代码',
          dataIndex: 'REPAIR_TYPE_CODE',
          hidden: true
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          width: 150,
          sorter: true
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME',
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '开单日期',
          dataIndex: 'RO_CREATE_DATE',
          showOverflowTooltip: true,
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '工单状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          filterItems: this.createDictList('8049'),
          render: props => {
            return <span>{this.createDictText(props.row.RO_STATUS, '8049')}</span>;
          }
        },
        {
          title: '服务顾问',
          dataIndex: 'sa',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          editType:'select',
          editItems:[]
        },
        {
          title: '预交车时间',
          dataIndex: 'END_TIME_SUPPOSED',
          showOverflowTooltip: true,
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '送修人',
          dataIndex: 'DELIVERER',
          hidden: true
        },
        {
          title: '送修人电话',
          dataIndex: 'DELIVERER_PHONE',
          hidden: true
        },
        {
          title: '送修人手机',
          dataIndex: 'DELIVERER_MOBILE',
          hidden: true
        },
        {
          title: '责任技师',
          dataIndex: 'ct',
          hidden: true
        }
      ];
    },
    setDefultCondition(vinData){
      this.topFilterList.find(item => item.fieldName === 'vin').initialValue = this.vinData.vin;
      this.topFilterList.find(item => item.fieldName === 'license').initialValue = this.vinData.license;
    },

    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 取消
    cancle() {
      this.$emit('cancle');
    },
    // 确定选择工单
    req() {
      if (this.choose.length === 0) {
        this.$message.error('请选择一个工单');
        return;
      }
      this.$emit('req', this.choose[0]);
    },
    // 获取全部员工
    async getEmp() {
      const res = await getDealerUser(this.editComplaintId);
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'sa').editItems = empList;
    }
  }
};
</script>
